home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / ASTRONOM / H139.ZIP / UI101.ZIP / IO / DR / DR_IBMPC.C next >
C/C++ Source or Header  |  1991-11-04  |  4KB  |  179 lines

  1. /*************************************************************************
  2.  
  3.     dr_ibmpc.c      Directory (dr) subroutines
  4.             for Bywater Software.
  5.  
  6.             Implementation for the IBM PC (tm)
  7.             and compatible microcomputers
  8.             utilizing Microsoft QuickC (tm)
  9.  
  10.             Copyright (c) 1991, Ted A. Campbell
  11.  
  12.             Bywater Software
  13.             P. O. Box 4023 
  14.             Duke Station 
  15.             Durham, NC  27706
  16.  
  17.             email: tcamp@hercules.acpub.duke.edu
  18.  
  19.     Copyright and Permissions Information:
  20.  
  21.     All U.S. and international copyrights are claimed by the
  22.     author. The author grants permission to use this code
  23.     and software based on it under the following conditions:
  24.     (a) in general, the code and software based upon it may be 
  25.     used by individuals and by non-profit organizations; (b) it
  26.     may also be utilized by governmental agencies in any country,
  27.     with the exception of military agencies; (c) the code and/or
  28.     software based upon it may not be sold for a profit without
  29.     an explicit and specific permission from the author, except
  30.     that a minimal fee may be charged for media on which it is
  31.     copied, and for copying and handling; (d) the code must be 
  32.     distributed in the form in which it has been released by the
  33.     author; and (e) the code and software based upon it may not 
  34.     be used for illegal activities. 
  35.  
  36. **************************************************************************/
  37.  
  38. #include "stdio.h"
  39. #include "dos.h"
  40. #include "sys\types.h"
  41. #include "sys\stat.h"
  42. #include "dr.h"
  43.  
  44. int     dr_fs = '\\';
  45. char    dr_all[ MAX_PATHLENGTH ] = "*.*";
  46.  
  47. static struct   find_t myfind;
  48. static char      _path[ MAX_PATHLENGTH ];
  49.  
  50. dr_first( findb, retb )
  51.     char findb[];
  52.     struct dir_ent *retb;
  53.     {
  54.     register int c;
  55.  
  56.     if ( _dos_findfirst( findb, _A_NORMAL | _A_SUBDIR, &myfind ) == 0 )
  57.         {
  58.         strcpy( retb->filename, myfind.name );
  59.         strcpy( _path, findb );
  60.  
  61.         c = strlen( _path );
  62.         while (( c >= 0 ) && ( _path[ c ] != dr_fs ))
  63.             {
  64. #ifdef  OLD_DEBUG
  65.             fprintf( stderr, "_path: %s \n", _path );
  66. #endif
  67.             _path[ c ] = 0;
  68.             --c;
  69.             }
  70.  
  71.         strcpy( retb->pathname, _path );
  72.  
  73.         fill_buffer( retb );
  74.         return 1;
  75.         }
  76.     else
  77.         {
  78.         return 0;
  79.         }
  80.     }
  81.  
  82. dr_next( retb )
  83.     struct dir_ent *retb;
  84.     {
  85.     if ( _dos_findnext( &myfind ) == 0 )
  86.         {
  87.         strcpy( retb->filename, myfind.name );
  88.         strcpy( retb->pathname, _path );
  89.         fill_buffer( retb );
  90.         return 1;
  91.         }
  92.     else
  93.         {
  94.         return 0;
  95.         }
  96.     }
  97.  
  98.  
  99. fill_buffer( retb )
  100.     struct dir_ent *retb;
  101.     {
  102.     static struct stat s_buf;
  103.     static char newpath[ MAX_PATHLENGTH ];
  104.  
  105.     if ( strlen( _path ) > 0  )
  106.         {
  107.         sprintf( newpath, "%s%s", _path, retb->filename );
  108.         }
  109.     else
  110.         {
  111.         strcpy( newpath, retb->filename );
  112.         }
  113.  
  114.     if ( stat( newpath, &s_buf ) < 0 )
  115.         {
  116. #ifdef  DEBUG
  117.         fprintf( stderr, "stat() to %s failed. \n", newpath );
  118. #endif
  119.         }
  120.  
  121.     retb->size = s_buf.st_size;
  122.  
  123.     if ( ibm_isexec( retb ) == TRUE )
  124.         {
  125.         retb->type = FILE_EXECUTABLE;
  126.         }
  127.  
  128.     else if ( ( s_buf.st_mode & S_IFDIR ) == S_IFDIR )
  129.         {
  130.         retb->type = FILE_DIRECTORY;
  131.         }
  132.  
  133.     else
  134.         {
  135.         retb->type = FILE_NORMAL;
  136.         }
  137.  
  138.     return TRUE;
  139.  
  140.     }
  141.  
  142. ibm_isexec( retb )
  143.     struct dir_ent *retb;
  144.     {
  145.     static char buffer[ MAX_PATHLENGTH ];
  146.     register int c, d;
  147.  
  148.     if ( strlen( retb->filename ) < 5 )
  149.         {
  150.         return FALSE;
  151.         }
  152.  
  153.     d = 0;
  154.     for ( c = strlen( retb->filename ) - 4;
  155.         c < strlen( retb->filename ); ++c )
  156.         {
  157.         buffer[ d ] = retb->filename[ c ];
  158.         ++d;
  159.         buffer[ d ] = 0;
  160.         }
  161.  
  162.     if ( strcmp( buffer, ".EXE" ) == 0 )
  163.         {
  164.         return TRUE;
  165.         }
  166.     else if ( strcmp( buffer, ".COM" ) == 0 )
  167.         {
  168.         return TRUE;
  169.         }
  170.     else if ( strcmp( buffer, ".BAT" ) == 0 )
  171.         {
  172.         return TRUE;
  173.         }
  174.     else
  175.         {
  176.         return FALSE;
  177.         }
  178.     }
  179.